package com.mzj.saas.mservice.eligible.repository;

import com.mzj.saas.mservice.eligible.entity.Eligible;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface EligibleRepository extends PagingAndSortingRepository<Eligible, Long>, JpaSpecificationExecutor<Eligible> {
    @Query(value = "SELECT * FROM  saas_house_eligible_apply WHERE match_user_id = ? AND apply_status != 0 order by apply_time desc", nativeQuery = true)
    List<Eligible> findByMatchUserId(String matchUserId);

    @Query(value = "SELECT * FROM  saas_house_eligible_apply WHERE org_id = ? AND apply_status != 0", nativeQuery = true)
    List<Eligible> findByOrgId(Long orgId);

    @Modifying
    @Query(value = "UPDATE `saas_house_eligible_apply` l SET l.apply_status = ?2 WHERE l.`id` = ?1", nativeQuery = true)
    int updateById(Long id, Integer status);

    @Modifying
    @Query(value = "UPDATE `saas_house_eligible_apply` l SET l.match_status = ?2 WHERE l.`id` = ?1", nativeQuery = true)
    int updateMatchStatus(Long id, Integer status);

    List<Eligible> findByIdIn(List<Long> ids);

    List<Eligible> findByCardNumber(String cardNumber);

    @Modifying
    @Query(value = "UPDATE `saas_house_eligible_apply` l SET l.apply_status = ?2 WHERE l.`id` in (?1)", nativeQuery = true)
    int updateApplyStatusByIds(List<Long> ids, Integer status);

    @Modifying
    @Query(value = "UPDATE `saas_house_eligible_apply` l SET l.apply_status = ?2 ,l.match_status = ?3 WHERE l.`id` in (?1)", nativeQuery = true)
    int updateApplyStatusAndMatchStatusByIds(List<Long> ids, Integer applyStatus, Integer matchStatus);

    @Modifying
    @Query(value = "UPDATE `saas_house_eligible_apply` l SET l.apply_status = ?2 ,l.match_status = ?3 WHERE l.`id` = ?1", nativeQuery = true)
    int updateApplyStatusAndMatchStatusById(Long id, Integer applyStatus, Integer matchStatus);

    @Query(value = "SELECT * FROM  saas_house_eligible_apply WHERE match_user_id = ? AND apply_status = 3 and match_status = 2 order by apply_time desc", nativeQuery = true)
    List<Eligible> findByMatchUserIdAndMathStatus(String matchUserId);


}
